package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.mine.contract.UserInfoContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerInfo;
import com.meitianhui.convenitenbuy.module.mine.presenter.UserInfoPresenter;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.widget.dialog.BottomMenuDialog;
import com.meitianhui.convenitenbuy.widget.dialog.MaterialDialog;
import com.meitianhui.convenitenbuy.widget.dialog.PhotoAndSexSelectDialog;
import com.meitianhui.convenitenbuy.widget.wheelview.TimeSelectorPopWindow;

import javax.inject.Inject;

import static com.meitianhui.convenitenbuy.Constants.CLICK_USER_BIRTHDAY;
import static com.meitianhui.convenitenbuy.Constants.CLICK_USER_NAME;
import static com.meitianhui.convenitenbuy.Constants.CLICK_USER_NICKNAME;
import static com.meitianhui.convenitenbuy.Constants.CLICK_USER_SEX;

/**
 *  Created by amy on 2017/11/3.
 */

public class UserInfoActivity extends BaseToolbarActivity implements UserInfoContract.View {

    private RelativeLayout mRlUserPhoto;
    private RelativeLayout mRlUserQrcode;
    private TextView mTvUserPhoneNumber;
    private RelativeLayout mRlUserPhoneNumber;
    private TextView mTvUserNickname;
    private RelativeLayout mRlUserNickname;
    private TextView mTvUserName;
    private RelativeLayout mRlUserName;
    private TextView mTvUserSex;
    private RelativeLayout mRlUserSex;
    private TextView mTvUserBirthday;
    private RelativeLayout mRlUserBirthday;
    @Inject
    UserInfoPresenter mUserInfoPresenter;
    private BottomMenuDialog mBottomMenuDialog;
    private ImageView mIvUserPhoto;
    private String mUserPhoneNumber;
    private MaterialDialog mEtTextMaterialDialog;
    private String mInputNewName;
    private PhotoAndSexSelectDialog sexDialog;



    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle("我的资料");
        //不需要刷新
        isEnable(false);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);
        Intent intent = getIntent();
        mUserPhoneNumber = intent.getStringExtra("userPhoneNumber");
        initId();
        initRxClick1();
        showUserInfo();
        getUserInfoFromServer();
        initBottomDialog();
    }

    private void getUserInfoFromServer() {
        mUserInfoPresenter.getLatestConsumerInfo();
    }

    private void createListTextDialog(String title, int clickItem) {
        mEtTextMaterialDialog = new MaterialDialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_text_input, null);
        TextView dialogInputText = (TextView) view.findViewById(R.id.et_name);
        mEtTextMaterialDialog.setContentView(view);
        mEtTextMaterialDialog.setTitle(title);
        mEtTextMaterialDialog.setPositiveButton(getResources().getString(R.string.dialog_confirm), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtTextMaterialDialog.dismiss();
                String filterNewName = mUserInfoPresenter.stringFilter(dialogInputText.getText().toString().trim());
                if (clickItem == CLICK_USER_NICKNAME) {
                    mTvUserNickname.setText(filterNewName);
                    mUserInfoPresenter.upDateLocalConsumer(filterNewName, CLICK_USER_NICKNAME);
                } else if (clickItem == CLICK_USER_NAME) {
                    mTvUserName.setText(filterNewName);
                    mUserInfoPresenter.upDateLocalConsumer(filterNewName, CLICK_USER_NAME);
                }
            }
        });
        mEtTextMaterialDialog.setNegativeButton(getResources().getString(R.string.dialog_cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtTextMaterialDialog.dismiss();
            }
        });
        mEtTextMaterialDialog.show();

    }


    private void showUserInfo() {

        mTvUserPhoneNumber.setText(mUserPhoneNumber);
        GlideUtils.setUserCircleImage(this,InfoProvider.getConsumerInfo(App.getInstance()).getHead_pic_path(), mIvUserPhoto);
        mTvUserNickname.setText(InfoProvider.getConsumerInfo(App.getInstance()).getNick_name());
        mTvUserName.setText(InfoProvider.getConsumerInfo(App.getInstance()).getFull_name());
        mTvUserSex.setText(getSex(InfoProvider.getConsumerInfo(App.getInstance()).getSex_key()));
        mTvUserBirthday.setText(InfoProvider.getConsumerInfo(App.getInstance()).getBirthday());

    }

    private String getSex(String sex_key) {
        if (sex_key.equals(Constants.SEX_MAN_KEY)){
            sexIndex = 0;
            return "男";
        }else if (sex_key.equals(Constants.SEX_WOMAN_KEY)){
            sexIndex = 1;
            return "女";
        }else {
            sexIndex = 2;
            return "保密";
        }
    }


    protected void initRxClick1() {
        super.initRxClick();
        RxUtils.bindEvents(mRlUserBirthday, this);
        RxUtils.bindEvents(mRlUserPhoto, this);
        RxUtils.bindEvents(mRlUserQrcode, this);
        RxUtils.bindEvents(mRlUserNickname, this);
        RxUtils.bindEvents(mRlUserName, this);
        RxUtils.bindEvents(mRlUserSex, this);
    }


    private void initBottomDialog() {
        mBottomMenuDialog = new BottomMenuDialog(this);
        mBottomMenuDialog.addItem(0, getResources().getString(R.string.user_select_photo_resource), null);
        mBottomMenuDialog.addItem(1, getResources().getString(R.string.user_select_camera), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开相机
                mUserInfoPresenter.openCamera();
            }
        });
        mBottomMenuDialog.addItem(2, getResources().getString(R.string.user_select_photo_album), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开相册
                mUserInfoPresenter.openAlbum();
            }
        });
        mBottomMenuDialog.addItem(3, getResources().getString(R.string.user_select_cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog();
            }
        });
        mBottomMenuDialog.setTextColor(0, getResources().getColor(R.color.gray));
        mBottomMenuDialog.setTextColor(3, getResources().getColor(R.color.gray));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mUserInfoPresenter.dealWithPicture(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.rl_user_photo:
                mBottomMenuDialog.show();
                break;
            case R.id.rl_user_qrcode:
                //点击二维码界面
                JumpUtil.jumpToActivityWithPhoneNumber(this, QRCodeActivity.class);
                break;
            case R.id.rl_user_nickname:
                createListTextDialog(getResources().getString(R.string.input_you_nickname), CLICK_USER_NICKNAME);
                break;
            case R.id.rl_user_name:
                createListTextDialog(getResources().getString(R.string.input_you_name), CLICK_USER_NAME);
                break;
            case R.id.rl_user_sex:
                choosingSex();
                break;
            case R.id.rl_user_birthday:
                choosingBirthday();
                break;
        }
    }
    //new一个占位的textView
    private void choosingBirthday() {
        TextView placeHolderTv = new TextView(this);
        TimeSelectorPopWindow timeSelectorPopWindow = new TimeSelectorPopWindow(mTvUserBirthday, placeHolderTv, this, new TimeSelectorPopWindow.DateSelectorPopWindowListener() {
            @Override
            public void onSelectDate(String dateStr, long date) {
                mUserInfoPresenter.upDateLocalConsumer(mTvUserBirthday.getText().toString(),CLICK_USER_BIRTHDAY);
            }
        });
        timeSelectorPopWindow.show(mTvUserBirthday);
    }

    private int sexIndex ;
    private void choosingSex() {
        sexDialog = new PhotoAndSexSelectDialog(this);
        sexDialog.setTitle("请选择性别");
        String[] sex = {"男", "女", "保密"};

        //设置一个单项选择下拉框
        /**
         * 第一个参数指定我们要显示的一组下拉单选框的数据集合
         * 第二个参数代表索引，指定默认哪一个单选框被勾选上，1表示默认'女' 会被勾选上
         * 第三个参数给每一个单选项绑定一个监听器
         */
        sexDialog.setSingleChoiceItems(sex, sexIndex, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                mTvUserSex.setText(sex[which]);
                if (which == 0){
                    sexIndex = 0;
                    mUserInfoPresenter.upDateLocalConsumer(Constants.SEX_MAN_KEY,CLICK_USER_SEX);
                }else if (which == 1){
                    sexIndex = 1;
                    mUserInfoPresenter.upDateLocalConsumer(Constants.SEX_WOMAN_KEY,CLICK_USER_SEX);
                }else {
                    sexIndex = 2;
                    mUserInfoPresenter.upDateLocalConsumer(Constants.SEX_PRIVATE_KEY,CLICK_USER_SEX);
                }
                dialog.dismiss();
            }
        });
        sexDialog.show();
    }
    @Override
    protected int setView() {
        return R.layout.frament_user_imformation;
    }

    protected void initId() {
        mRlUserBirthday = getViewById(R.id.rl_user_birthday);
        mRlUserPhoto = getViewById(R.id.rl_user_photo);
        mRlUserQrcode = getViewById(R.id.rl_user_qrcode);
        mRlUserPhoneNumber = getViewById(R.id.rl_user_Phone_number);
        mRlUserNickname = getViewById(R.id.rl_user_nickname);
        mRlUserName = getViewById(R.id.rl_user_name);
        mRlUserSex = getViewById(R.id.rl_user_sex);
        mTvUserPhoneNumber = getViewById(R.id.tv_user_phone_number);
        mTvUserNickname = getViewById(R.id.tv_user_nickname);
        mTvUserName = getViewById(R.id.tv_user_name);
        mTvUserSex = getViewById(R.id.tv_user_sex);
        mTvUserBirthday = getViewById(R.id.tv_user_birthday);
        mIvUserPhoto = getViewById(R.id.iv_user_photo);
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {

    }
    @Override
    public void dismissDialog() {
        mBottomMenuDialog.dismiss();
    }

    @Override
    public void setUserPhoto(String doc_path, String doc_id) {
        GlideUtils.setUserCircleImage(this,doc_path, mIvUserPhoto);
    }

    @Override
    public void upDateUserInfo() {
        String userNickName = mTvUserNickname.getText().toString().trim();
        String userSex = InfoProvider.getConsumerInfo(App.getInstance()).getSex_key();
        String userBirthday = mTvUserBirthday.getText().toString();
        String userName = mTvUserName.getText().toString();
        mUserInfoPresenter.upLoadUserInfo(userNickName, userSex, userBirthday, userName, InfoProvider.getConsumerInfo(this).getHead_pic_path());
    }

    @Override
    public void getLatestConsumerInfoSucc(ConsumerInfo consumerInfo) {
        PreferenceUtils.saveObject(this,Constants.CONSUMER_INFO,consumerInfo);
        GlideUtils.setUserCircleImage(this,InfoProvider.getConsumerInfo(App.getInstance()).getHead_pic_path(), mIvUserPhoto);
        mTvUserNickname.setText(InfoProvider.getConsumerInfo(App.getInstance()).getNick_name());
        mTvUserName.setText(InfoProvider.getConsumerInfo(App.getInstance()).getFull_name());
        mTvUserSex.setText(getSex(InfoProvider.getConsumerInfo(App.getInstance()).getSex_key()));
        mTvUserBirthday.setText(InfoProvider.getConsumerInfo(App.getInstance()).getBirthday());
    }



}
